[TOC]

Characters

A character array can be created by a pair of single quotes. For example, 'character array' is a vector of 15 characters (including the white space).

Characters are often used to display information. A character array can also be used as an input argument of a function. For example, one can use the character array '*-r' in plot(x,y,'*-r') to specify the line and marker style of a plot.

The character type can be converted to a numeric type. For example, a character is implicitly converted to a double when it is used in an arithmetic operation such as 'a' + 1. It means adding the ASCII code of a (which is 97) to 1. Conversely, an integer lying in a suitable range can be converted to a character.

⚠️ In MATLAB, double quotes are used to create strings. However, string type is not supported yet in SIMO, and hence a double-quote " is not recognisable by SIMO.

Unicode Support

It is possible to create a character array with unicode characters. For example, a = '😃🤘🚙' returns

a = 
😃🤘🚙

Each of the elements in the character array can be accessed by indexes just like an array of numbers. For example, a(1) returns

ans = 
😃

The size of an array vector is determined by the number of characters in each dimension. Therefore, size(a) gives [1, 3].

For some Unicode characters, e.g., 😃, they are encoded by a sequence 16-bit integers, known as scalar values. Hence, when the character '😃' is converted to doubles by double(a(1)), it returns a row vector of length 2:

ans = 
 55357.   56835.

As another example, double('simile 😃, yeah 🤘, car 🚙') returns the vector below:

ans = 
Columns 1 through 6:
 115.00   105.00   109.00   105.00   108.00   101.00
Columns 7 through 12:
 32.000   55357.   56835.   44.000   32.000   121.00
Columns 13 through 18:
 101.00   97.000   104.00   32.000   55358.   56600.
Columns 19 through 24:
 44.000   32.000   99.000   97.000   114.00   32.000
Columns 25 through 26:
 55357.   56985.

Although there are only 23 characters in the character array, there are 26 numbers encoding these characters. From Columns 1 to 6, they are ASCII codes of simile. Then, in Column 7, 32 is the ASCII code of the white space. From Columns 8 to 9, they are the scalar values encoding 😃. Similarly, for Columns 17 - 18 and 25 - 26, these are scalar values encoding 🤘 and 🚙.

Arithmetic with Unicode Character

In the following example, we add an emoji character to some numbers. The character is first converted to numbers by SIMO before added to 1. The conversion results in a row vector of two integers (see output of Line 2). The addition operations in Lines 3 and 4 work without error since the scalar 1, the vector [1, 2] and the integer vector converted from '😃' all have compatible sizes.

Input
c = '😃';
double(c)
'😃' + 1
'😃' + [1 2]
Output
ans = 
 55357.   56835.

ans = 
 55358.   56836.

ans = 
 55358.   56837.

Data Type Conversion

Data of all numeric types, including integer, double and logical, can be converted to characters using built-in functions, such as char(), num2str and int2Str.

In the following example, we define the variables a that stores the characters 'good'. Then, the characters are converted to doubles using double(). These doubles are finally converted back to characters 'good'.

Input
a = 'good';
d = double(a)
char(d)
Output
d = 
 103.00   111.00   111.00   100.00

ans = 
good

User should pay attention to the following situations when converting numbers to character array:

  1. The given numbers do not correspond to a character.
  2. The given numbers are converted to character that cannot be printed.

In the code below, we convert the integers 0 - 255 to characters. As shown in the output,

Input
char(0:255)
Output
ans = 
Columns 1 through 73:
          
                      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGH
Columns 74 through 146:
IJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~                   
Columns 147 through 219:
               ¡¢£¤¥¦§¨©ª«¬ ®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚ
Columns 220 through 256:
ÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ

In Line 2 below, we convert '香港🇭🇰' to a sequence of UTF-16 code units using double(). The first two integers in d correspond to 香港 and the last four integers in d correspond to the flag 🇭🇰. Then, the whole sequence is converted back to '香港🇭🇰' using char() in Line 3. In Line 4, however, only the first four code units are converted. As a result, the flag character 🇭🇰 is truncated and becomes 🇭.

Input
hk = '香港🇭🇰'
d = double(hk)
char(d)
char(d(1:4))
Output
hk = 
香港🇭🇰

d = 
 39321.   28207.   55356.   56813.   55356.   56816.

ans = 
香港🇭🇰

ans = 
香港🇭